﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Employees_EmployeesInfoStatistics : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


        ////X坐标轴标识数组
        ////Labels on X Axis
        //ArrayList XTitle = new ArrayList();
        //XTitle.Add("East");
        //XTitle.Add("South");
        //XTitle.Add("West");
        //XTitle.Add("North");
        //XTitle.Add("NorthEast");
        //XTitle.Add("SouthWest");
        //XTitle.Add("NorthWest");
        //XTitle.Add("East");
        //XTitle.Add("South");
        //XTitle.Add("West");
        //XTitle.Add("North");
        //XTitle.Add("NorthEast");
        //XTitle.Add("SouthWest");
        //XTitle.Add("NorthWest");


        ////图表数据（1组）
        ////Chart Data
        //ArrayList[] ChartData = new ArrayList[1];

        //ChartData[0] = new ArrayList();
        //ChartData[0].Add(80);
        //ChartData[0].Add(40);
        //ChartData[0].Add(1130);
        //ChartData[0].Add(24);
        //ChartData[0].Add(22);
        //ChartData[0].Add(201);
        //ChartData[0].Add(112);
        //ChartData[0].Add(80);
        //ChartData[0].Add(40);
        //ChartData[0].Add(1130);
        //ChartData[0].Add(24);
        //ChartData[0].Add(22);
        //ChartData[0].Add(201);
        //ChartData[0].Add(112);

        //Chartlet1.InitializeData(ChartData, XTitle, null);

        try
        {
            if (!IsPostBack)
            {
                string Show = Request["ItemParam"].ToString();
                EmpStatusCount.Visible = false;
                EmpStatusBar.Visible = false;
                switch (Show)
                {

                    case "worktime":
                        BindWorkTimeCount();
                        break;
                    case "age":
                        BindAgeCount();
                        break;
                    case "acc":
                        BindAccCount();
                        break;
                    case "edu":
                        BindEduCount();
                        break;
                    case "sex":
                        BindSexCount();
                        break;
                    case "title":
                        BindTitleCount();
                        break;
                    case "empstatus":
                        BindEmpStatusCount();
                        break;
                    default:
                        Show = "sex";
                        BindSexCount();
                        break;
                }

                ((HyperLink)MenuTabs.FindControl(Show)).CssClass = "OnHover";
                Chartlet1.Visible = true;

            

                MasterPage_MasterPage master = (MasterPage_MasterPage)this.Page.Master;
                master.HelpHref = "";
                master.PageName = "人事统计";
                master.PicUrl = "~/image/PageIcon/tongji.jpg";
                master.IntiBar();
            }
        }
        catch
        {
            Chartlet1.Visible = false;
        }

    }


    private void BindAgeCount()
    {

        ArrayList XTitle = new ArrayList();

        ArrayList[] ChartData = new ArrayList[1];

        object[] oj = Srvtools.CliUtils.CallMethod("SEmployees", "GetEmpAgeCount", new object[] { null });

        DataTable dt = ((DataSet)oj[1]).Tables[0];

        int[] age = new int[13];

        int AgeCount = 0;

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (dt.Rows[i]["years"] == DBNull.Value || dt.Rows[i]["years"].ToString() == "")
            {
                age[0] += Convert.ToInt32(dt.Rows[i]["counts"]);
            }
            else if (Convert.ToInt32(dt.Rows[i]["years"]) >= 0 && Convert.ToInt32(dt.Rows[i]["years"]) <= 15)
            {
                age[1] += Convert.ToInt32(dt.Rows[i]["counts"]);
            }
            else if (Convert.ToInt32(dt.Rows[i]["years"]) >= 16 && Convert.ToInt32(dt.Rows[i]["years"]) <= 20)
            {
                age[2] += Convert.ToInt32(dt.Rows[i]["counts"]);
            }
            else if (Convert.ToInt32(dt.Rows[i]["years"]) >= 21 && Convert.ToInt32(dt.Rows[i]["years"]) <= 25)
            {
                age[3] += Convert.ToInt32(dt.Rows[i]["counts"]);
            }
            else if (Convert.ToInt32(dt.Rows[i]["years"]) >= 26 && Convert.ToInt32(dt.Rows[i]["years"]) <= 30)
            {
                age[4] += Convert.ToInt32(dt.Rows[i]["counts"]);
            }
            else if (Convert.ToInt32(dt.Rows[i]["years"]) >= 31 && Convert.ToInt32(dt.Rows[i]["years"]) <= 35)
            {
                age[5] += Convert.ToInt32(dt.Rows[i]["counts"]);
            }
            else if (Convert.ToInt32(dt.Rows[i]["years"]) >= 36 && Convert.ToInt32(dt.Rows[i]["years"]) <= 40)
            {
                age[6] += Convert.ToInt32(dt.Rows[i]["counts"]);
            }
            else if (Convert.ToInt32(dt.Rows[i]["years"]) >= 41 && Convert.ToInt32(dt.Rows[i]["years"]) <= 45)
            {
                age[7] += Convert.ToInt32(dt.Rows[i]["counts"]);
            }
            else if (Convert.ToInt32(dt.Rows[i]["years"]) >= 46 && Convert.ToInt32(dt.Rows[i]["years"]) <= 50)
            {
                age[8] += Convert.ToInt32(dt.Rows[i]["counts"]);
            }
            else if (Convert.ToInt32(dt.Rows[i]["years"]) >= 51 && Convert.ToInt32(dt.Rows[i]["years"]) <= 55)
            {
                age[9] += Convert.ToInt32(dt.Rows[i]["counts"]);
            }
            else if (Convert.ToInt32(dt.Rows[i]["years"]) >= 56 && Convert.ToInt32(dt.Rows[i]["years"]) <= 60)
            {
                age[10] += Convert.ToInt32(dt.Rows[i]["counts"]);
            }
            else if (Convert.ToInt32(dt.Rows[i]["years"]) >= 61 && Convert.ToInt32(dt.Rows[i]["years"]) <= 65)
            {
                age[11] += Convert.ToInt32(dt.Rows[i]["counts"]);
            }
            else if (Convert.ToInt32(dt.Rows[i]["years"]) >= 66)
            {
                age[12] += Convert.ToInt32(dt.Rows[i]["counts"]);
            }

            AgeCount += Convert.ToInt32(dt.Rows[i]["counts"]);
        }

        XTitle.Add("NULL" + ConvertShow(age[0], AgeCount));
        XTitle.Add("0-15" + ConvertShow(age[1], AgeCount));
        XTitle.Add("16-20" + ConvertShow(age[2], AgeCount));
        XTitle.Add("21-25" + ConvertShow(age[3], AgeCount));
        XTitle.Add("26-30" + ConvertShow(age[4], AgeCount));
        XTitle.Add("31-35" + ConvertShow(age[5], AgeCount));
        XTitle.Add("36-40" + ConvertShow(age[6], AgeCount));
        XTitle.Add("41-45" + ConvertShow(age[7], AgeCount));
        XTitle.Add("46-50" + ConvertShow(age[8], AgeCount));
        XTitle.Add("51-55" + ConvertShow(age[9], AgeCount));
        XTitle.Add("56-60" + ConvertShow(age[10], AgeCount));
        XTitle.Add("60-65" + ConvertShow(age[11], AgeCount));
        XTitle.Add(">65" + ConvertShow(age[12], AgeCount));

        ChartData[0] = new ArrayList();
        ChartData[0].Add(age[0]);
        ChartData[0].Add(age[1]);
        ChartData[0].Add(age[2]);
        ChartData[0].Add(age[3]);
        ChartData[0].Add(age[4]);
        ChartData[0].Add(age[5]);
        ChartData[0].Add(age[6]);
        ChartData[0].Add(age[7]);
        ChartData[0].Add(age[8]);
        ChartData[0].Add(age[9]);
        ChartData[0].Add(age[10]);
        ChartData[0].Add(age[11]);
        ChartData[0].Add(age[12]);




        Chartlet1.ChartTitle.Text = "员工年龄统计";
        Chartlet1.YLabels.UnitText = "人";
        Chartlet1.XLabels.UnitText = "岁";

        Chartlet1.InitializeData(ChartData, XTitle, null);
    }


    private void BindWorkTimeCount()
    {
        ArrayList XTitle = new ArrayList();

        ArrayList[] ChartData = new ArrayList[1];

        ChartData[0] = new ArrayList();

        object[] oj = Srvtools.CliUtils.CallMethod("SEmployees", "GetEmpWorktimedateCount", new object[] { null });

        DataTable dt = ((DataSet)oj[1]).Tables[0];

        int AllCount = 0;

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            AllCount += Convert.ToInt32(dt.Rows[i]["counts"]);
        }

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (dt.Rows[i]["years"] == DBNull.Value || dt.Rows[i]["years"].ToString() == "")
            {
                XTitle.Add("无" + ConvertShow(Convert.ToInt32(dt.Rows[i]["counts"]), AllCount));

            }
            else
            {

                XTitle.Add(dt.Rows[i]["years"].ToString() + ConvertShow(Convert.ToInt32(dt.Rows[i]["counts"]), AllCount));

            }
            ChartData[0].Add(dt.Rows[i]["counts"].ToString());

        }



        Chartlet1.ChartTitle.Text = "员工工龄统计";
        Chartlet1.YLabels.UnitText = "人";
        Chartlet1.XLabels.UnitText = "年";
        Chartlet1.ChartType = FanG.Chartlet.ChartTypes.HBar;
        Chartlet1.Height = Unit.Pixel(1200);
        Chartlet1.InitializeData(ChartData, XTitle, null);
    }

    private void BindAccCount()
    {
        ArrayList XTitle = new ArrayList();

        ArrayList[] ChartData = new ArrayList[1];

        ChartData[0] = new ArrayList();

        object[] oj = Srvtools.CliUtils.CallMethod("SEmployees", "GetEmpAccessionCount", new object[] { null });

        DataTable dt = ((DataSet)oj[1]).Tables[0];
        int AllCount = 0;

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            AllCount += Convert.ToInt32(dt.Rows[i]["counts"]);
        }

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (dt.Rows[i]["years"] == DBNull.Value || dt.Rows[i]["years"].ToString() == "")
            {
                XTitle.Add("NULL" + ConvertShow(Convert.ToInt32(dt.Rows[i]["counts"]), AllCount));


            }
            else
            {
                XTitle.Add(dt.Rows[i]["years"].ToString() + ConvertShow(Convert.ToInt32(dt.Rows[i]["counts"]), AllCount));
            }
            ChartData[0].Add(dt.Rows[i]["counts"].ToString());
        }



        Chartlet1.ChartTitle.Text = "员工在职时间统计";
        Chartlet1.YLabels.UnitText = "人";
        Chartlet1.XLabels.UnitText = "年";
        Chartlet1.InitializeData(ChartData, XTitle, null);
    }

    private void BindEduCount()
    {
        ArrayList XTitle = new ArrayList();

        ArrayList[] ChartData = new ArrayList[1];

        ChartData[0] = new ArrayList();

        object[] oj = Srvtools.CliUtils.CallMethod("SEmployees", "GetEmpEducationCount", new object[] { null });

        DataTable dt = ((DataSet)oj[1]).Tables[0];

        DataTable dts = ((DataSet)oj[2]).Tables[0];

        int AllCount = 0;

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            AllCount += Convert.ToInt32(dt.Rows[i]["Column1"]);
        }

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (dt.Rows[i]["Education"] == DBNull.Value || dt.Rows[i]["Education"].ToString() == "")
            {
                XTitle.Add("NULL" + ConvertShow(Convert.ToInt32(dt.Rows[i]["Education"]), AllCount));
            }
            else
            {
                for (int j = 0; j < dts.Rows.Count; j++)
                {
                    if (dts.Rows[j]["ValueId"].ToString() == dt.Rows[i]["Education"].ToString())
                    {
                        XTitle.Add(dts.Rows[j]["ValueName"].ToString() + ConvertShow(Convert.ToInt32(dt.Rows[i]["Column1"]), AllCount));
                    }
                }
            }
            ChartData[0].Add(dt.Rows[i]["Column1"].ToString());
        }


        Chartlet1.ChartTitle.Text = "员工学历统计";
        Chartlet1.YLabels.UnitText = "人";
        Chartlet1.XLabels.UnitText = "学历类型";
        Chartlet1.InitializeData(ChartData, XTitle, null);
    }


    private void BindSexCount()
    {
        ArrayList XTitle = new ArrayList();

        ArrayList[] ChartData = new ArrayList[1];

        ChartData[0] = new ArrayList();

        object[] oj = Srvtools.CliUtils.CallMethod("SEmployees", "GetEmpSexCount", new object[] { null });

        DataTable dt = ((DataSet)oj[1]).Tables[0];

        int AllCount = 0;

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            AllCount += Convert.ToInt32(dt.Rows[i]["counts"]);
        }

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (dt.Rows[i]["sex"] == DBNull.Value)
            {
                XTitle.Add("NULL" + ConvertShow(Convert.ToInt32(dt.Rows[i]["counts"]), AllCount));
            }
            else if (Convert.ToBoolean(dt.Rows[i]["sex"]) == true)
            {
                XTitle.Add("男性");
            }
            else if (Convert.ToBoolean(dt.Rows[i]["sex"]) == false)
            {
                XTitle.Add("女性");
            }
            ChartData[0].Add(dt.Rows[i]["counts"].ToString());
        }



        Chartlet1.ChartTitle.Text = "员工人数统计[总 " + AllCount.ToString() + " 人]";
        Chartlet1.ChartType = FanG.Chartlet.ChartTypes.Pie;
        //Chartlet1.YLabels.UnitText = "总 " + AllCount.ToString() + " 人";
        //Chartlet1.XLabels.UnitText = "类型";
        Chartlet1.InitializeData(ChartData, XTitle, null);
    }

    private void BindTitleCount()
    {
        ArrayList XTitle = new ArrayList();

        ArrayList[] ChartData = new ArrayList[1];

        ChartData[0] = new ArrayList();

        object[] oj = Srvtools.CliUtils.CallMethod("SEmployees", "GetEmpTitleCount", new object[] { null });

        DataTable dt = ((DataSet)oj[1]).Tables[0];

        DataTable dts = ((DataSet)oj[2]).Tables[0];

        int AllCount = 0;

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            AllCount += Convert.ToInt32(dt.Rows[i]["Column1"]);
        }

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (dt.Rows[i]["Title"] == DBNull.Value || dt.Rows[i]["Title"].ToString() == "")
            {
                XTitle.Add("NULL" + ConvertShow(Convert.ToInt32(dt.Rows[i]["Title"]), AllCount));
            }
            else
            {
                for (int j = 0; j < dts.Rows.Count; j++)
                {
                    if (dts.Rows[j]["ValueId"].ToString() == dt.Rows[i]["Title"].ToString())
                    {
                        XTitle.Add(dts.Rows[j]["ValueName"].ToString() + ConvertShow(Convert.ToInt32(dt.Rows[i]["Column1"]), AllCount));
                    }
                }
            }
            ChartData[0].Add(dt.Rows[i]["Column1"].ToString());
        }


        Chartlet1.ChartTitle.Text = "员工职称统计";
        Chartlet1.YLabels.UnitText = "人";
        Chartlet1.XLabels.UnitText = "职称类型";

        Chartlet1.InitializeData(ChartData, XTitle, null);
    }


    private string ConvertShow(int small, int big)
    {
        return " [ " + Convert.ToDouble(Convert.ToDouble(small) / Convert.ToDouble(big)).ToString("0%") + " ]";
    }

    #region 员工状态统计
    private void BindEmpStatusCount()
    {
        EmpStatusCount.Visible = true;
        General.Visible = false;
        Chart.Visible = false;
    }

    protected void StatusSearchButton_Click(object sender, EventArgs e)
    {
        string year = SelYear.Text;
        string month = SelMonth.Text;
        object[] obj = null;
        if (year == "")
        {
            return;
        }

        Chart.Visible = true;
        EmpStatusBar.Visible = true;
        if (month == "")
        {
            obj = new object[] { "y", year, "0", SelType.SelectedValue };
        }
        else
        {
            obj = new object[] { "m", year, month, SelType.SelectedValue };
        }


        object[] ret = Srvtools.CliUtils.CallMethod("SEmployees", "EmpWorkOnCount", obj);
        BindStatusPie(ret, SelType.SelectedValue);
    }

    private void BindStatusPie(object[] ret, string Type)
    {
        try
        {
            //int AllCount = 0;
            ArrayList XTitle = new ArrayList();

            ArrayList[] ChartData = new ArrayList[1];
            ChartData[0] = new ArrayList();

            ArrayList BTitle = new ArrayList();

            ArrayList[] BChartData = new ArrayList[1];
            BChartData[0] = new ArrayList();


            EmpStatusPie.ChartTitle.Text = "员工统计 TOP 5";
            EmpStatusPie.ChartType = FanG.Chartlet.ChartTypes.Pie;

            EmpStatusBar.ChartTitle.Text = "员工统计 全部";
            EmpStatusBar.ChartType = FanG.Chartlet.ChartTypes.Bar;
            EmpStatusBar.YLabels.UnitText = "人数";
            EmpStatusBar.XLabels.UnitText = "类型";


            DataTable dt = new DataTable();
            DataTable dtt = new DataTable();
            Hashtable ht = new Hashtable();
            switch (Type)
            {
                case "1":

                    dt = ((DataSet)ret[1]).Tables[0];
                    ChartData[0].Add(dt.Rows[0]["AccessionCount"].ToString());
                    XTitle.Add("入职人数");
                    ChartData[0].Add(dt.Rows[0]["DimissionCount"].ToString());
                    XTitle.Add("离职人数");
                    ChartData[0].Add(dt.Rows[0]["WorkCount"].ToString());
                    XTitle.Add("在职人数");


                    BChartData[0].Add(dt.Rows[0]["AccessionCount"].ToString());
                    BTitle.Add("入职人数");
                    BChartData[0].Add(dt.Rows[0]["DimissionCount"].ToString());
                    BTitle.Add("离职人数");
                    BChartData[0].Add(dt.Rows[0]["WorkCount"].ToString());
                    BTitle.Add("在职人数");
                    EmpStatusPie.ChartTitle.Text = "员工统计";
                    break;
                case "2":
                    dt = ((DataSet)ret[1]).Tables[0];
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        ChartData[0].Add(dt.Rows[i]["Ecount"].ToString());
                        XTitle.Add(dt.Rows[i]["Type"].ToString());

                        if (i < 5)
                        {
                            BChartData[0].Add(dt.Rows[i]["Ecount"].ToString());
                            BTitle.Add(dt.Rows[i]["Type"].ToString());
                        }
                    }
                    break;
                case "3":
                    dt = ((DataSet)ret[2]).Tables[0];
                    dtt = ((DataSet)ret[3]).Tables[0];
                    //GetCheckListBind
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        string[] Rz = GetCheckListBind(Convert.ToInt32(dt.Rows[i]["Type"].ToString())).ToString().Split(',');
                        for (int m = 0; m < Rz.Length; m++)
                        {
                            DataRow[] dr = dtt.Select("ValueId='" + Rz[m].ToString() + "'");
                            if (dr.Length > 0)
                            {
                                if (ht[Rz[m]] == null)
                                {
                                    ht.Add(Rz[m].ToString(), Convert.ToInt32(dt.Rows[i]["Ecount"].ToString()));
                                }
                                else
                                {

                                    int temp = Convert.ToInt32(ht[Rz[m]]);
                                    ht.Remove(Rz[m]);
                                    ht.Add(Rz[m], temp + Convert.ToInt32(dt.Rows[i]["Ecount"].ToString()));
                                }
                            }

                        }
                    }

                    for (int j = 0; j < dtt.Rows.Count; j++)
                    {
                        if (ht[dtt.Rows[j]["ValueId"].ToString()] != null)
                        {
                            ChartData[0].Add(ht[dtt.Rows[j]["ValueId"].ToString().ToString()]);
                            XTitle.Add(dtt.Rows[j]["ValueName"].ToString());

                            BChartData[0].Add(ht[dtt.Rows[j]["ValueId"].ToString()].ToString());
                            BTitle.Add(dtt.Rows[j]["ValueName"].ToString());

                        }
                    }
                    EmpStatusPie.ChartTitle.Text = "员工统计";
                    break;
                case "4":
                    dt = ((DataSet)ret[2]).Tables[0];
                    dtt = ((DataSet)ret[3]).Tables[0];
                    //GetCheckListBind
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        for (int j = 0; j < dtt.Rows.Count; j++)
                        {
                            if (dt.Rows[i]["Type"].ToString() == dtt.Rows[j]["ValueId"].ToString())
                            {
                                ChartData[0].Add(dt.Rows[i]["Ecount"].ToString());
                                XTitle.Add(dtt.Rows[j]["ValueName"].ToString());

                                BChartData[0].Add(dt.Rows[i]["Ecount"].ToString());
                                BTitle.Add(dtt.Rows[j]["ValueName"].ToString());

                                break;

                            }
                        }
                    }

                    EmpStatusPie.ChartTitle.Text = "员工统计";
                    break;
                case "5":
                    dt = ((DataSet)ret[1]).Tables[0];
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        ChartData[0].Add(dt.Rows[i]["Ecount"].ToString());
                        XTitle.Add(dt.Rows[i]["Type"].ToString());

                        if (i < 5)
                        {
                            BChartData[0].Add(dt.Rows[i]["Ecount"].ToString());
                            BTitle.Add(dt.Rows[i]["Type"].ToString());
                        }
                    }
                    break;
            }

            if (ChartData.Length > 0)
            {
                EmpStatusBar.InitializeData(ChartData, XTitle, null);
                EmpStatusPie.InitializeData(BChartData, BTitle, null);
            }
        }
        catch
        { }
    }

    private string GetCheckListBind(int Reason)
    {
        int Rid = Reason;
        int RoundInt = 1;
        string Back = "";

        while (RoundInt > 0)
        {
            if (RoundInt * 2 < Rid)
            {
                RoundInt = RoundInt * 2;
            }
            else if (Rid <= 2 || RoundInt * 2 == Rid)
            {



                Back += Rid + ",";
                RoundInt = -1;
            }

            else
            {

                Back += RoundInt + ",";
                Rid = Rid - RoundInt;
                RoundInt = 1;
            }
        }
        return Back.Substring(0, Back.Length - 1); ;
    }

    #endregion
}
